package com.yanqu.road.dao.impl.activity.daomu;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserCollectionData;
import com.yanqu.road.entity.activity.daomu.data.DaoMuActivityUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DaoMuUserCollectionDataImpl extends TempDataObjectDao<DaoMuActivityUserCollectionData> {

    public static String selectSql = "select * from `t_u_activity_daomu_user_collection_data` where `activityId`= ? and userId = ?";
    /**
     * 查询
     * @param activityId
     * @return
     */
    public Map<Integer, DaoMuActivityUserCollectionData> getDaoMuActivityUserCollectionData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DaoMuActivityUserCollectionData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                DaoMuActivityUserCollectionData data = parse(rs);
                map.put(data.getCollectionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertSql = "INSERT INTO `t_u_activity_daomu_user_collection_data`(`activityId`, `userId`, `collectionId`, `collectionLevel`, `collectionPropNum`, `createTime`) VALUES (?, ?, ?, ?, ?, ?)";
    /**
     * 插入
     * @param data 数据对象
     * @return
     */
    @Override
    protected boolean addData(DaoMuActivityUserCollectionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCollectionId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCollectionLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCollectionPropNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_u_activity_daomu_user_collection_data` SET `collectionLevel` = ?, `collectionPropNum` = ?  WHERE `activityId` = ? AND `userId` = ? and `collectionId` = ?";
    /**
     * 更新
     * @param data
     * @return
     */
    @Override
    protected boolean updateData(DaoMuActivityUserCollectionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getCollectionLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCollectionPropNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCollectionId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 解析数据
     * @param rs
     * @return
     * @throws SQLException
     */
    @Override
    protected DaoMuActivityUserCollectionData parseData(ResultSet rs) throws SQLException {
        DaoMuActivityUserCollectionData data = new DaoMuActivityUserCollectionData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setCollectionId(rs.getInt("collectionId"));
        data.setCollectionLevel(rs.getInt("collectionLevel"));
        data.setCollectionPropNum(rs.getInt("collectionPropNum"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }
}
